Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:pagernav runat=server></{0}:pagernav>")> Public Class PagerNav
        Inherits System.Web.UI.WebControls.WebControl
        Public Enum Mode
            Links
            CallBack
        End Enum
        Dim _PageCount As Integer
        Dim _CurrentPage As Integer
        Dim _Text As String
        Dim _ForumID As Integer = 0
        Dim _TopicId As Integer
        Dim _TabID As Integer
        Dim _PageText As String
        Dim _OfText As String
        Dim _View As String
        Dim _Params As String()
        Dim _Optional2 As String = ""
        Dim _ClientScript As String = "afPage({0});"
        Dim _Mode As Mode
        Dim _useShortUrls As Boolean = False

        Public Property UseShortUrls() As Boolean
            Get
                Return _useShortUrls
            End Get
            Set(ByVal value As Boolean)
                _useShortUrls = value
            End Set
        End Property
        Public Property [PageMode]() As Mode
            Get
                Return _Mode
            End Get
            Set(ByVal value As Mode)
                _Mode = value
            End Set
        End Property
        Public Property BaseURL() As String = String.Empty
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [PageCount]() As Integer
            Get
                Return _PageCount
            End Get

            Set(ByVal Value As Integer)
                _PageCount = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [ForumID]() As Integer
            Get
                Return _ForumID
            End Get

            Set(ByVal Value As Integer)
                _ForumID = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("0")> Property [TopicId]() As Integer
            Get
                Return _TopicId
            End Get

            Set(ByVal Value As Integer)
                _TopicId = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [TabID]() As Integer
            Get
                Return _TabID
            End Get

            Set(ByVal Value As Integer)
                _TabID = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String
            Get
                Return _Text
            End Get

            Set(ByVal Value As String)
                _Text = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [View]() As String
            Get
                Return _View
            End Get

            Set(ByVal Value As String)
                _View = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("Page:")> Property [PageText]() As String
            Get
                Return _PageText
            End Get

            Set(ByVal Value As String)
                _PageText = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("of")> Property [OfText]() As String
            Get
                Return _OfText
            End Get

            Set(ByVal Value As String)
                _OfText = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [CurrentPage]() As Integer
            Get
                Return _CurrentPage
            End Get

            Set(ByVal Value As Integer)
                _CurrentPage = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Params]() As String()
            Get
                Return _Params
            End Get

            Set(ByVal Value As String())
                _Params = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Optional2]() As String
            Get
                Return _Optional2
            End Get

            Set(ByVal Value As String)
                _Optional2 = Value
            End Set
        End Property
        Public Property ClientScript() As String
            Get
                Return _ClientScript
            End Get
            Set(ByVal value As String)
                _ClientScript = value
            End Set
        End Property

        Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
            _Text = RenderPager()
            output.Write(_Text)
        End Sub
        Public Function RenderPager() As String
            Dim sb As New Text.StringBuilder
            Dim iMaxPage As Integer = [CurrentPage] + 2
            If iMaxPage > [PageCount] Then
                iMaxPage = [PageCount]
            End If
            Dim i As Integer = 1
            Dim iStart As Integer = 1
            If HttpContext.Current.Request.Browser.IsMobileDevice Then
                PageMode = Mode.Links
            End If
            PageMode = Mode.Links
            If Not String.IsNullOrEmpty(BaseURL) Then
                If Not BaseURL.EndsWith("/") Then
                    BaseURL &= "/"
                End If
                If Not BaseURL.StartsWith("/") Then
                    BaseURL = "/" & BaseURL
                End If
            End If
            Dim qs As String = String.Empty
            If Not HttpContext.Current.Request.QueryString("ts") Is Nothing Then
                qs = "?ts=" & HttpContext.Current.Request.QueryString("ts")
            End If
            'Dim iEnd As Integer
            If [PageCount] > 1 Then

                sb.Append("<table cellpadding=""0"" cellspacing=""0"" border=""0"" class=""afpager""><tr>")
                sb.Append("<td class=""af_pager"">" & [PageText] & " " & [CurrentPage] & " " & [OfText] & " " & [PageCount] & "</td>")
                If Not [CurrentPage] = 1 Then
                    If PageMode = Mode.Links Then
                        If String.IsNullOrEmpty(BaseURL) Then
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & Utilities.NavigateUrl([TabID], "", BuildParams([View], [ForumID], 1, [TopicId])) & """ title=""First Page""> &lt;&lt; </a></td>")
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & Utilities.NavigateUrl([TabID], "", BuildParams([View], [ForumID], [CurrentPage] - 1, [TopicId])) & """ title=""Previous Page""> &lt; </a></td>")
                        Else
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & BaseURL & qs & """ title=""First Page""> &lt;&lt; </a></td>")
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & BaseURL & ([CurrentPage] - 1) & "/" & qs & """ title=""Previous Page""> &lt; </a></td>")
                        End If


                    Else
                        sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""javascript:" & String.Format(ClientScript, "1").ToString & """ title=""First Page""> &lt;&lt; </a></td>")
                        sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""javascript:" & String.Format(ClientScript, [CurrentPage] - 1).ToString & """ title=""Previous Page""> &lt; </a></td>")

                    End If


                    'Else
                    '    iMaxPage = iMaxPage + 1
                End If
                If [CurrentPage] <= 3 Then
                    iStart = 1
                    iMaxPage = 5
                Else
                    iStart = [CurrentPage] - 2
                    iMaxPage = [CurrentPage] + 2
                End If

                If iMaxPage > PageCount Then
                    iMaxPage = PageCount
                End If
                If iMaxPage = PageCount Then
                    iStart = iMaxPage - 4
                End If
                If iStart <= 0 Then
                    iStart = 1
                End If
                For i = iStart To iMaxPage
                    If i = [CurrentPage] Then
                        sb.Append("<td class=""af_currentpage"" style=""text-align:center;"">" & i & "</td>")
                    Else
                        If PageMode = Mode.Links Then
                            If String.IsNullOrEmpty(BaseURL) Then
                                sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & Utilities.NavigateUrl(TabID, "", BuildParams([View], [ForumID], i, [TopicId])) & """>" & i & "</a></td>")
                            Else

                                If i > 1 Then
                                    sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & BaseURL & i & "/" & qs & """>" & i & "</a></td>")
                                Else
                                    sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & BaseURL & qs & """>" & i & "</a></td>")
                                End If

                            End If

                        Else
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""javascript:" & String.Format(ClientScript, i).ToString & """>" & i & "</a></td>")
                        End If

                    End If
                    If i = [PageCount] Then
                        Exit For
                    End If
                Next
                If Not [CurrentPage] = [PageCount] Then
                    If PageMode = Mode.Links Then
                        If String.IsNullOrEmpty(BaseURL) Then
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & Utilities.NavigateUrl([TabID], "", BuildParams([View], [ForumID], [CurrentPage] + 1, [TopicId])) & """ title=""Next Page""> &gt;</a></td>")
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & Utilities.NavigateUrl([TabID], "", BuildParams([View], [ForumID], [PageCount], [TopicId])) & """ title=""Last Page""> &gt;&gt;</a></td>")
                        Else
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & BaseURL & ([CurrentPage] + 1) & "/" & qs & """ title=""Next Page""> &gt;</a></td>")
                            sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""" & BaseURL & PageCount & "/" & qs & """ title=""Last Page""> &gt;&gt;</a></td>")
                        End If

                    Else
                        sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""javascript:" & String.Format(ClientScript, [CurrentPage] + 1).ToString & """ title=""Next Page""> &gt;</a></td>")
                        sb.Append("<td class=""af_pagernumber"" style=""text-align:center;""><a href=""javascript:" & String.Format(ClientScript, [PageCount]).ToString & """> &gt;&gt;</a></td>")
                    End If

                End If
                sb.Append("</tr></table>")
            End If
            Return sb.ToString
        End Function
        Private Function BuildParams(ByVal View As String, ByVal ForumID As Integer, ByVal Page As Integer, Optional ByVal PostID As Integer = 0) As String()
            Dim Params2 As String() = New String() {}
            Select Case View.ToLowerInvariant
                Case Views.Topics.ToLowerInvariant
                    If Page > 1 Then
                        Params2 = New String() {ParamKeys.ViewType & "=" & View, ParamKeys.ForumId & "=" & [ForumID], ParamKeys.PageId & "=" & Page}
                    Else
                        Params2 = New String() {ParamKeys.ViewType & "=" & View, ParamKeys.ForumId & "=" & [ForumID]}
                    End If

                    If UseShortUrls And Page > 1 Then
                        Params2 = New String() {ParamKeys.ForumId & "=" & [ForumID], ParamKeys.PageId & "=" & Page}
                    ElseIf UseShortUrls And Page = 1 Then
                        Params2 = New String() {ParamKeys.ForumId & "=" & [ForumID]}
                    End If
                    If Not [Params] Is Nothing Then
                        Dim intLength As Integer = Params2.Length
                        Array.Resize(Params2, (intLength + [Params].Length))
                        [Params].CopyTo(Params2, intLength)
                    End If

                Case Views.Topic.ToLowerInvariant
                    If Page > 1 Then
                        Params2 = New String() {ParamKeys.ViewType & "=" & View, ParamKeys.ForumId & "=" & [ForumID], ParamKeys.TopicId & "=" & PostID, ParamKeys.PageId & "=" & Page}
                    Else
                        Params2 = New String() {ParamKeys.ViewType & "=" & View, ParamKeys.ForumId & "=" & [ForumID], ParamKeys.TopicId & "=" & PostID}
                    End If

                    If UseShortUrls And Page > 1 Then
                        Params2 = New String() {ParamKeys.TopicId & "=" & PostID, ParamKeys.PageId & "=" & Page}
                    ElseIf UseShortUrls And Page = 1 Then
                        Params2 = New String() {ParamKeys.TopicId & "=" & PostID}
                    End If
                    If Not [Params] Is Nothing Then
                        Dim intLength As Integer = Params2.Length
                        Array.Resize(Params2, (intLength + [Params].Length))
                        [Params].CopyTo(Params2, intLength)
                    End If
                Case Else
                    If Not [Params] Is Nothing Then
                        Params2 = New String() {ParamKeys.ViewType & "=" & View, ParamKeys.PageId & "=" & Page}
                        Dim intLength As Integer = Params2.Length
                        Array.Resize(Params2, (intLength + [Params].Length))
                        [Params].CopyTo(Params2, intLength)
                    Else
                        Params2 = New String() {ParamKeys.ViewType & "=" & View, ParamKeys.PageId & "=" & Page}
                    End If
            End Select
            Return Params2
        End Function

        Private Sub PagerNav_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Me.EnableViewState = False
        End Sub
    End Class
End Namespace